package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.*;
import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;
import com.epfresh.api.utils.*;

public class JoinPrepareGridViewBinder extends ItemViewBinder<JoinPrepareGrid.SupplyProductDtoBean.ItemsBean, JoinPrepareGridViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public String chargeUnit = "斤";

    public String getChargeUnit() {
        return chargeUnit;
    }

    public void setChargeUnit(String chargeUnit) {
        this.chargeUnit = chargeUnit;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    //更新某个位置的数据
    public void notifyItem(int position) {
        if (position > -1 && position < getAdapter().getItemCount())
            getAdapter().notifyItemChanged(position);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_join_prepare_grid, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JoinPrepareGrid.SupplyProductDtoBean.ItemsBean post) {

        int position = getPosition(holder);
        holder.ll_all.setTag(R.id.item_key_position, position);
        holder.ll_all.setTag(R.id.item_key_status, post);
        holder.ll_all.setOnClickListener(this);
        holder.tv_number_1.setText(post.getQuantity()+chargeUnit);
        holder.tv_number_comment.setText(post.getQuantity()+chargeUnit);

        if(post.getOrderNumber()!=null&&post.getOrderNumber().length()>6) {
            if(post.getPosition()!=null&&post.getPosition().length()>0)
                holder.tv_number_3.setText(post.getOrderNumber().substring(post.getOrderNumber().length() - 6, post.getOrderNumber().length()) +"("+post.getPosition()+")");
            else
                holder.tv_number_3.setText(post.getOrderNumber().substring(post.getOrderNumber().length() - 6, post.getOrderNumber().length()));
        }else{
            if(post.getPosition()!=null&&post.getPosition().length()>0)
                holder.tv_number_3.setText(post.getOrderNumber() +"("+post.getPosition()+")");
            else
                holder.tv_number_3.setText(post.getOrderNumber());
        }
        if(post.getName()==null||"null".equals(post.getName())||"".equals(post.getName())){
            holder.tv_name.setText("收件人");
        }else {
            holder.tv_name.setText(post.getName());
        }


        if(post.getActualQuantity() == null){
            holder.tv_number_1.setTextSize(20);
            holder.tv_number_2.setVisibility(View.GONE);
            holder.ll_content.setEnabled(true);
        }else {
            holder.tv_number_1.setTextSize(14);
            holder.tv_number_2.setText(post.getActualQuantity()+chargeUnit);
            holder.tv_number_2.setVisibility(View.VISIBLE);
            holder.ll_content.setEnabled(false);
            holder.ll_comment.setVisibility(View.GONE);
        }

        if(post.getMerMsg()!=null&&!"".equals(post.getMerMsg())){
            //holder.tv_number_1.setTextSize(14);
            holder.tv_comment.setText(post.getMerMsg());
            holder.ll_comment.setVisibility(View.VISIBLE);
        }else {
            //holder.tv_number_1.setTextSize(22);
            holder.ll_comment.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            JoinPrepareGrid.SupplyProductDtoBean.ItemsBean bean = (JoinPrepareGrid.SupplyProductDtoBean.ItemsBean)o;
          if (v.getId() == R.id.ll_all) {
              bean.setMerMsg(null);
              notifyItem(position);
          }
        }
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_number_comment;

        TextView tv_comment;
        TextView tv_number_1;
        TextView tv_number_2;
        TextView tv_number_3;
        View ll_all,ll_content,ll_comment;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_all =  itemView.findViewById(R.id.ll_all);
            ll_content =  itemView.findViewById(R.id.ll_content);
            ll_comment =  itemView.findViewById(R.id.ll_comment);
            tv_comment = itemView.findViewById(R.id.tv_comment);
            tv_number_1 = itemView.findViewById(R.id.tv_number_1);
            tv_number_2 = itemView.findViewById(R.id.tv_number_2);
            tv_number_3 = itemView.findViewById(R.id.tv_number_3);

            tv_name = itemView.findViewById(R.id.tv_name);
            tv_number_comment = itemView.findViewById(R.id.tv_number_comment);

        }
    }
}
